﻿using DAL;
using FubiManger.UI;
using Model;
using Smobiler.Core;
using Smobiler.Core.Controls;
using Smobiler.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace FubiManger.Controls
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class ordercontrol1 : Smobiler.Core.Controls.MobileUserControl
    {
        public ordercontrol1() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }
        /// <summary>
        /// 昵称
        /// </summary>
        public string nickname { get; set; }
        /// <summary>
        /// 订单号
        /// </summary>
        public string ordsn { get; set; }
        /// <summary>
        /// 订单时间
        /// </summary>
        public string ordtime { get; set; }
        /// <summary>
        /// 头像
        /// </summary>
        public string uheard { get; set; }
        /// <summary>
        /// 餐桌号
        /// </summary>
        public string disksn { get; set; }
        public string ordid { get; set; }
        /// <summary>
        /// 订单状态
        /// </summary>
        public string codestatus { get; set; }
        /// <summary>
        /// 1首页2待核销
        /// </summary>
        public string stype { get; set; }
        private void btninfo_Press(object sender, EventArgs e)
        {
            OrdinfoForm frm = new OrdinfoForm();
            frm.ordsn = ordsn;//订单编号
            this.Form.Show(frm, true, (obj, args) =>
            {
                if (stype == "2")
                    //待核销
                    ((ClearcodeForm)this.Form).Refsh();//刷新
                //else
                //    ((MainForm)this.Form).Getdata();//刷新数据
            });
        }

        private void btncodes_Press(object sender, EventArgs e)
        {
            
        }

        private void ordercontrol1_Load(object sender, EventArgs e)
        {
           
            if (!string.IsNullOrEmpty(disksn))
            {
                lbldisksn.Text = "餐桌号：" + disksn;//餐桌号
                lbldisksn.Visible = true;
            }
            lblordsn.Text = "订单号：" + ordsn;//订单号
            lbltime.Text = ordtime;//订单时间
            lbltel.Text = nickname;//昵称
        }

        private void btnprint_Press(object sender, EventArgs e)
        {
            msgcontrol con = new msgcontrol();
            con.title = "打印提示";
            con.content = "您确定需要补打该订单小票吗?";
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (con.flag == true)
                {

                    ms_order order = orderDal.Querybysn(ordsn);
                    ms_merchant merchant = merchantDal.Querybyid(order.merchant_id);
                    int count = 0;
                    string printtxt = merchant.merchant_name + "\r\n";
                    string str2 = "收货人：" + order.consignee_name;
                    string str6 = "电话：" + order.consignee_tel.Substring(0, 3) + "*****" + order.consignee_tel.Substring(8, 3);
                    string str3 = "---------------------";
                    string str4 = "时间：" + order.add_time;
                    string str5 = "编号：" + order.order_sn.Substring(8);
                    string str7 = "---------------------";
                    string str10 = "---------------------";
                    string str11 = "优惠：-￥" + order.coupon_amount;
                    string str12 = "配送：￥" + order.logistics_amount;
                    string str13 = "打包：￥" + order.packing_amount;
                    string str14 = "实付：￥" + order.order_price;
                    string str15 = "星鲜厨 正儿八经吃个饭";
                    string str16 = "备注：" + order.orderremark;

                    List<string> strlist = new List<string>();

                    strlist.Add(str2);
                    strlist.Add(str3);
                    strlist.Add(str4);
                    strlist.Add(str5);
                    strlist.Add(str6);
                    strlist.Add(str7);
                    JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
                    List<Gdpro> prolist = js.Deserialize<List<Gdpro>>(order.goods_info);

                    for (int i = 0; i < prolist.Count; i++)
                    {
                        string proname = "";
                        char paddingChar = ' '; // 指定空白字符为空格
                        int totalLength = 8 - prolist[i].proname.Length; // 指定字符串加空白后的总长度
                        if (prolist[i].proname.Length < 8)
                            proname = prolist[i].proname.PadRight(totalLength, paddingChar);
                        string pstr = proname + "  x" + prolist[i].count + " ￥" + prolist[i].price;
                        strlist.Add(pstr);
                    }

                    strlist.Add(str10);
                    strlist.Add(str11);
                    strlist.Add(str12);
                    strlist.Add(str13);
                    strlist.Add(str14);
                    strlist.Add(str15);
                    if (!string.IsNullOrEmpty(order.orderremark))
                        strlist.Add(str16);
                    for (int i = 0; i < strlist.Count; i++)
                    {
                        //count = (i + 1) * 35;
                        printtxt += strlist[i] + "\r\n";
                    }
                }
            });
            
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="printxt"></param>
        private void Print(Object printxt)
        {
            //string ptstr = printxt.ToString();
            //P390PrinterEntityCollection Commands1 = new P390PrinterEntityCollection();
            //Commands1.Add(new P390PrinterRawProtocolEntity("SIZE 57 mm, 15 mm\r\n" + "REFERENCE 0,0\r\n" + "SPEED 4.0\r\n" + "DENSITY 8\r\n" + "SET PEEL OFF\r\n" + "SET CUTTER OFF\r\n" + "SET TEAR ON\r\n" + "DIRECTION 0\r\n" + "SHIFT 0\r\n" + "OFFSET 0 mm\r\n" + "CLS\r\n" + ptstr + "PRINT 1,1\r\n"));

            //p390Printer1.Print(Commands1, (obj, args) =>
            //{
            //    if (args.isError == true)
            //        this.Toast("Error: " + args.error);
            //    else
            //    {

            //    }
            //});
            P800PrintEntityCollection entityblueslection = new P800PrintEntityCollection();

            //文本打印
            P800PrintTextEntity text = new P800PrintTextEntity()
            {
                Content = printxt.ToString(),
                Offset = P800PrintOffSet.Left,
                IsBold = false,
                CharacterMultipleX = 1,
                CharacterMultipleY = 1,
                IsUnderLine = false,

            };
            entityblueslection.Add(text);
            //第一个参数：打印指令集，第二个参数：是否切纸，第三个参数：回调
            p800Printer1.Print(entityblueslection, true, (obj, args) =>
            {
                if (args.isError == true)
                    this.MessageBox.Show("Error: " + args.error);
                else
                    this.MessageBox.Show("success");

            });
        }
    }
}